{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "da128c24",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[2, 2, 2, 2]\n",
      "----------------------------------------------------------------------------------------------------\n",
      "[1, 2, 1, 2, 1, 2, 1, 2, 1, 1, 1] 序列长度： 11\n",
      "[2, 1, 2, 1, 2, 1, 2, 1, 1, 1] 序列长度： 10\n",
      "----------------------------------------------------------------------------------------------------\n",
      "[2, 1, 2, 1, 2, 1, 2, 1, 1, 1] 序列长度： 10\n",
      "[2, 2, 1, 2, 1, 2, 1, 1, 1] 序列长度： 9\n",
      "----------------------------------------------------------------------------------------------------\n",
      "[2, 2, 1, 2, 1, 2, 1, 1, 1] 序列长度： 9\n",
      "[2, 2, 2, 1, 2, 1, 1, 1] 序列长度： 8\n",
      "----------------------------------------------------------------------------------------------------\n",
      "[2, 2, 2, 1, 2, 1, 1, 1] 序列长度： 8\n",
      "[2, 2, 2, 2, 1, 1, 1] 序列长度： 7\n",
      "----------------------------------------------------------------------------------------------------\n",
      "[2, 2, 2, 2, 1, 1, 1] 序列长度： 7\n",
      "[2, 2, 2, 2, 1, 1] 序列长度： 6\n",
      "----------------------------------------------------------------------------------------------------\n",
      "[2, 2, 2, 2, 1, 1] 序列长度： 6\n",
      "[2, 2, 2, 2, 1] 序列长度： 5\n",
      "[2, 2, 2, 2, 1]\n"
     ]
    }
   ],
   "source": [
    "x=[1,2,1,2,1,2,1,2,1];\n",
    "for i in x:\n",
    "    if i==1:\n",
    "      x.remove(i);\n",
    "print(x)\n",
    "y=[1,2,1,2,1,2,1,2,1,1,1];#i表示序号，只会一直加1但是后面的1存在跳变，导致序号加1但是存在1个1不行\n",
    "for i in y:\n",
    "    print(\"--\"*50);\n",
    "    if i==1:\n",
    "      print(f\"{y}\",\"序列长度：\",len(y));\n",
    "      y.remove(i);\n",
    "      print(f\"{y}\",\"序列长度：\",len(y));\n",
    "print(y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "5fcb0c59",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "12>=10\n",
      "True\n",
      "12>=10\n",
      "True\n"
     ]
    }
   ],
   "source": [
    "# 1. 双引号包裹的字符串，print直接输出字符串字面内容\n",
    "print(\"12>=10\");  # 输出：12>=10\n",
    "\n",
    "# 2. 数值比较表达式（12大于等于10），print输出表达式的布尔结果（True/False）\n",
    "print(12>=10);    # 输出：True\n",
    "\n",
    "# 3. 单引号包裹的字符串，与双引号字符串功能完全一致，print直接输出字符串字面内容\n",
    "print('12>=10');  # 输出：12>=10\n",
    "\n",
    "# 4. 字符串之间的比较，按字符Unicode编码逐个对比（先比第一个字符，再比第二个...）\n",
    "#    '1'与'1'编码相同，'2'（编码50）大于'0'（编码48），因此整体表达式为True\n",
    "print('12'>='10');  # 输出：True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "1425fe48",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "20\n",
      "0\n",
      "----------------------------------------------------------------------------------------------------\n",
      "False\n",
      "0\n",
      "False\n",
      "True\n",
      "----------------------------------------------------------------------------------------------------\n",
      "20\n",
      "0\n",
      "0\n"
     ]
    }
   ],
   "source": [
    "# or运算：从左到右判断，返回第一个\"真\"值（非0即真）；若全为\"假\"，返回最后一个值\n",
    "print(1 or 20);  # 输出：1  → 1是\"真\"值，直接返回第一个真的值\n",
    "print(0 or 20);  # 输出：20 → 0是\"假\"值，继续判断下一个，20是\"真\"，返回20\n",
    "print(0 or 0);   # 输出：0  → 两个都是\"假\"值，返回最后一个值\n",
    "print(\"--\"*50)\n",
    "\n",
    "# and运算：从左到右判断，返回第一个\"假\"值（0为假）；若全为\"真\"，返回最后一个值\n",
    "# 先看比较表达式的结果，再进行逻辑运算\n",
    "print(12<=10 and 12>=10);  # 输出：False → 12<=10是False（假），直接返回第一个假值\n",
    "print(12<=10 or 0);\n",
    "print(12<=11 or 12<=10);\n",
    "print(12<=10 or 12>=10);   # 输出：True  → 12<=10是False，继续判断12>=10是True，返回True\n",
    "print(\"--\"*50)\n",
    "\n",
    "# 数值直接参与and运算\n",
    "print(1 and 20);  # 输出：20 → 1是\"真\"，继续判断下一个，20是\"真\"，返回最后一个真值\n",
    "print(0 and 20);  # 输出：0  → 0是\"假\"，直接返回第一个假值\n",
    "print(0 and 0);   # 输出：0  → 第一个0是\"假\"，直接返回第一个假值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "2bc5c42b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1 or 20 输出结果： 1\n",
      "0 or 20 输出结果： 20\n",
      "0 or 0 输出结果： 0\n",
      "----------------------------------------------------------------------------------------------------\n",
      "12<=10 and 12>=10 输出结果： False\n",
      "12<=10 and 0 输出结果： False\n",
      "12<=10 and 12<=11 输出结果： False\n",
      "12<=10 or 12<=11 输出结果： False\n",
      "12<=10 or 0 输出结果： 0\n",
      "12<=10 or 12>=10 输出结果： True\n",
      "----------------------------------------------------------------------------------------------------\n",
      "1 and 20 输出结果： 20\n",
      "0 and 20 输出结果： 0\n",
      "0 and 0 输出结果： 0\n"
     ]
    }
   ],
   "source": [
    "# or运算：从左到右判断，返回第一个\"真\"值（非0/True为真）；全假则返回最后一个值\n",
    "print(\"1 or 20 输出结果：\", 1 or 20);  # 1 or 20 输出结果：1 → 1是真值，直接返回第一个真值\n",
    "print(\"0 or 20 输出结果：\", 0 or 20);  # 0 or 20 输出结果：20 → 0是假值，继续判断下一个，20是真值，返回20\n",
    "print(\"0 or 0 输出结果：\", 0 or 0);    # 0 or 0 输出结果：0 → 全为假值，返回最后一个值\n",
    "\n",
    "\n",
    "print(\"--\"*50)  # 分隔线\n",
    "\n",
    "# and运算：从左到右判断，返回第一个\"假\"值（0/False为假）；全真则返回最后一个值\n",
    "# 先计算比较表达式的真假，再进行逻辑运算\n",
    "print(\"12<=10 and 12>=10 输出结果：\", 12<=10 and 12>=10);  # 12<=10 and 12>=10 输出结果：False → 12<=10是False（假），直接返回第一个假值\n",
    "\n",
    "# 12<=10是False（假），and运算遇到第一个假值就返回，与后面的值无关\n",
    "print(\"12<=10 and 0 输出结果：\", 12<=10 and 0);  # 12<=10 and 0 输出结果：False → 12<=10是假，直接返回该假值（不会判断后面的0）\n",
    "\n",
    "# 12<=10是False（假），and运算直接返回第一个假值\n",
    "print(\"12<=10 and 12<=11 输出结果：\", 12<=10 and 12<=11);  # 12<=10 and 12<=11 输出结果：False → 12<=10为假，无需判断后面的12<=11（12<=11本身也是假）\n",
    "\n",
    "# or运算：12<=10是False（假），继续判断12<=11（12<=11为False，因12>11），全假返回最后一个假值\n",
    "print(\"12<=10 or 12<=11 输出结果：\", 12<=10 or 12<=11);  # 12<=10 or 12<=11 输出结果：False\n",
    "\n",
    "# or运算：12<=10是False（假），0也是假值，全假返回最后一个值（0）\n",
    "print(\"12<=10 or 0 输出结果：\", 12<=10 or 0);  # 12<=10 or 0 输出结果：0\n",
    "\n",
    "# or运算：12<=10是False（假），12>=10是True（真），返回第一个真值（True）\n",
    "print(\"12<=10 or 12>=10 输出结果：\", 12<=10 or 12>=10);  # 12<=10 or 12>=10 输出结果：True\n",
    "\n",
    "\n",
    "print(\"--\"*50)  # 分隔线\n",
    "\n",
    "# 数值直接参与and运算（非0为真，0为假）\n",
    "print(\"1 and 20 输出结果：\", 1 and 20);  # 1 and 20 输出结果：20 → 1是真，继续判断下一个，20是真，返回最后一个真值\n",
    "print(\"0 and 20 输出结果：\", 0 and 20);  # 0 and 20 输出结果：0 → 0是假，直接返回第一个假值\n",
    "print(\"0 and 0 输出结果：\", 0 and 0);    # 0 and 0 输出结果：0 → 第一个0是假，直接返回第一个假值"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a0766c67",
   "metadata": {},
   "source": [
    "简化版 if else 语句，前面是其中一个输出结果，后面是判断条件，如果条件成立，则输出前面的结果，否则输出后面的结果。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "c1d1675a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "成年\n",
      "未成年\n"
     ]
    }
   ],
   "source": [
    "age=20;\n",
    "print(\"成年\"if age>=18 else \"未成年\");\n",
    "age=17;\n",
    "print(\"成年\"if age>=18 else \"未成年\");"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "6b4de759",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1 * 1 = 1\t\n",
      "1 * 2 = 2\t2 * 2 = 4\t\n",
      "1 * 3 = 3\t2 * 3 = 6\t3 * 3 = 9\t\n",
      "1 * 4 = 4\t2 * 4 = 8\t3 * 4 = 12\t4 * 4 = 16\t\n",
      "1 * 5 = 5\t2 * 5 = 10\t3 * 5 = 15\t4 * 5 = 20\t5 * 5 = 25\t\n",
      "1 * 6 = 6\t2 * 6 = 12\t3 * 6 = 18\t4 * 6 = 24\t5 * 6 = 30\t6 * 6 = 36\t\n",
      "1 * 7 = 7\t2 * 7 = 14\t3 * 7 = 21\t4 * 7 = 28\t5 * 7 = 35\t6 * 7 = 42\t7 * 7 = 49\t\n",
      "1 * 8 = 8\t2 * 8 = 16\t3 * 8 = 24\t4 * 8 = 32\t5 * 8 = 40\t6 * 8 = 48\t7 * 8 = 56\t8 * 8 = 64\t\n",
      "1 * 9 = 9\t2 * 9 = 18\t3 * 9 = 27\t4 * 9 = 36\t5 * 9 = 45\t6 * 9 = 54\t7 * 9 = 63\t8 * 9 = 72\t9 * 9 = 81\t\n"
     ]
    }
   ],
   "source": [
    "#  九九乘法表\n",
    "for i in range(1,10):#range函数左闭右开从1到9\n",
    "    for j in range(1,i+1):\n",
    "        print(j,\"*\",i,\"=\",i*j,end=\"\\t\")\n",
    "    print()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6c75cf09",
   "metadata": {},
   "source": [
    "# 字符串常用方法"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "132edbeb",
   "metadata": {},
   "source": [
    "### 一、字母大小写相关\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ef37434e",
   "metadata": {},
   "source": [
    "str.title()：每个单词首字母大写，其余字母小写（单词以非字母数字字符分隔）\n",
    "\n",
    "\n",
    "str.capitalize()：字符串首字符大写，其余字符小写\n",
    "\n",
    "\n",
    "str.upper()：所有字符转为大写\n",
    "\n",
    "\n",
    "str.lower()：所有字符转为小写\n",
    "\n",
    "\n",
    "str.swapcase()：字母大小写反转（大写转小写、小写转大写）\n",
    "\n",
    "str指需要改变格式的字符串"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "01194943",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "I am a jxufer\n",
      "I am a Jxufer\n",
      "I am a jxufer\n"
     ]
    }
   ],
   "source": [
    "# 定义变量advertisement，指向原始字符串对象\"I am a Jxufer\"\n",
    "advertisement = \"I am a Jxufer\"\n",
    "\n",
    "# 调用capitalize()生成临时字符串对象（内容为\"I am a jxufer\"）\n",
    "# 该对象仅在print()中被使用一次，因无变量引用，使用后会被垃圾回收\n",
    "print(advertisement.capitalize())\n",
    "\n",
    "# 打印变量advertisement指向的原始字符串（未被修改，因临时对象未被保存）\n",
    "print(advertisement)\n",
    "\n",
    "# 再次调用capitalize()生成新字符串对象，并将变量advertisement的引用指向该对象\n",
    "# 此时新对象被变量持有，不会被回收，可后续重复使用\n",
    "advertisement = advertisement.capitalize()\n",
    "\n",
    "# 打印变量现在指向的新字符串对象（因已被保存，所以能正常访问）\n",
    "print(advertisement)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cf1ca858",
   "metadata": {},
   "source": [
    "### 二：类型与编码相关"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a61edce1",
   "metadata": {},
   "source": [
    "str(s)\t将任意类型 s 转为字符串\n",
    "\n",
    "chr(u)\t返回 Unicode 编码 u 对应的单个字符 要是整数\n",
    "\n",
    "ord(c)\t返回单个字符 c 对应的 Unicode 编码（整数） 注意单个字符"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "fbd7ba4f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'int'> 123456\n",
      "123456\n",
      "<class 'str'> 49 123456\n"
     ]
    }
   ],
   "source": [
    "# 定义一个整数变量b，值为123456\n",
    "b = 123456\n",
    "# 打印变量b的类型（整数类型）和其值\n",
    "print(type(b), b)  # 输出：<class 'int'> 123456\n",
    "\n",
    "# 使用str()函数将整数b转换为字符串\n",
    "# 注意：转换后得到的是数字的文本形式（如97会变成\"97\"，而非chr(97)对应的\"a\"）\n",
    "b = str(b)  # 此时b变为字符串\"123456\"（由'1','2','3','4','5','6'六个字符组成）\n",
    "\n",
    "# 打印转换后的字符串b\n",
    "print(b)  # 输出：123456\n",
    "\n",
    "# 打印字符串b的类型、第一个字符的Unicode码点和字符串本身\n",
    "# ord(b[0])：取字符串第一个字符（这里是'1'），返回其对应的Unicode编码值（'1'的码点为49）\n",
    "print(type(b), ord(b[0]), b)  # 输出：<class 'str'> 49 123456"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ec573619",
   "metadata": {},
   "source": [
    "### 三、开头 / 结尾判断\n",
    "\n",
    "str.startswith(str1)\t判断是否以子串 str1 开头,可以在都好后面设置开始检测的数\n",
    "\n",
    "str.endswith(str1)\t判断是否以子串 str1 结尾"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "bf836c09",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "字符's'的位置：5\n",
      "字符'h'的位置：6\n",
      "字符'e'的位置：7\n",
      "字符'n'的位置：8\n",
      "字符'g'的位置：9\n",
      "b中的所有字符都在a中找到\n",
      "True\n",
      "True\n",
      "False\n"
     ]
    }
   ],
   "source": [
    "a = 'I am shengdeyunlong'\n",
    "b = 'sheng'\n",
    "count=0\n",
    "a = 'I am shengdeyunlong'\n",
    "b = 'sheng'  # 包含5个字符：'s','h','e','n','g'\n",
    "count = 0  # 计数找到的字符次数\n",
    "\n",
    "# 遍历b中的每个字符（共5个，索引0到4）\n",
    "for i in range(len(b)):\n",
    "    # 遍历a中的每个位置，检查是否以b[i]开头\n",
    "    for pos in range(len(a)):\n",
    "        # 检查a中pos位置是否以b的第i个字符开头\n",
    "        if a.startswith(b[i], pos):\n",
    "            count += 1  # 计数+1（Python中用+=1，不用++）\n",
    "            print(f\"字符'{b[i]}'的位置：{pos}\")  # 输出找到的字符和位置\n",
    "            break  # 找到后退出当前字符的查找循环\n",
    "\n",
    "    # 当计数达到5（即b中所有字符都找到），输出提示\n",
    "    if count == 5:\n",
    "        print(\"b中的所有字符都在a中找到\")\n",
    "        break\n",
    "\n",
    "# 从索引5开始（含5），检查a的子串是否以b开头（a[5:]为'shengdeyunlong'，与b完全一致）\n",
    "print(a.startswith(b, 5))  # 输出：True\n",
    "\n",
    "# 检查索引5到12（含5不含13）的子串是否以'sheng'开头（该范围子串为'shengdeyu'，前5个字符是'sheng'，但实际输出False，可能范围计算偏差）\n",
    "print(a.startswith('sheng', 5, 13))  # 输出：False\n",
    "\n",
    "# 检查索引6到10（含6不含11）的子串是否以'e'开头（该范围子串以'h'开头，不匹配'e'）\n",
    "print(a.startswith('e', 6, 11))  # 输出：False"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "54d770c7",
   "metadata": {},
   "source": [
    "### 四、两端字符删除\n",
    "str.rstrip(str1)\t删除末尾指定字符 str1，默认删空白字符（空格、换行、制表符等）//strip 剥离，去除\n",
    "\n",
    "str.lstrip(str1)\t删除前端指定字符 str1，默认删空白字符\n",
    "\n",
    "str.strip(str1)\t删除两端指定字符 str1，默认删空白字符"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "2a52cc1f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "shengdeyunlong.\n",
      "shengdeyunlong\n"
     ]
    }
   ],
   "source": [
    "a=\"shengdeyunlong.\"\n",
    "print(a)\n",
    "print(a.rstrip('.'))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ab505f4f",
   "metadata": {},
   "source": [
    "### 五、字符串对齐与填充\n",
    "\n",
    "str.center(len, str1)\t居中显示，总长度 len，两边填充 str1（默认填空格） len指示总长度 \n",
    "\n",
    "str.center(width, fillchar)：width = 居中后字符串的 “总长度”\n",
    "\n",
    "str.ljust(n, str1)\t居左显示，总长度 n，右侧填充 str1（默认填空格）\n",
    "\n",
    "str.rjust(n, str1)\t居右显示，总长度 n，左侧填充 str1（默认填空格）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "5163ec65",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "---shengdeyunlong--\n"
     ]
    }
   ],
   "source": [
    "a=\"shengdeyunlong\"\n",
    "print(a.center(len(a)+5,'-'))#这里只能是单个字符，然后根据左一个右一个来填充满字符，如5个字符左三右二"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1578f3a9",
   "metadata": {},
   "source": [
    "\n",
    "### 六、子串查找\n",
    "\n",
    "str.find(str1, start, end)\t查找子串 str1 在 [start, end) 内首次出现的索引，不存在返回 -1（区分大小写）\n",
    "\n",
    "str.rfind(str1, start, end)\t查找子串 str1 在 [start, end) 内最后一次出现的索引，不存在返回 -1（区分大小写）\n",
    "\n",
    "str.index(str1, start, end)\t功能同 find，但子串不存在时抛出异常\n",
    "\n",
    "str.rindex(str1, start, end)\t功能同 rfind，但子串不存在时抛出异常\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "47141899",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "5\n"
     ]
    }
   ],
   "source": [
    "a=\"shengdeyunlong\"\n",
    "print(a.find('de',0,len(a)))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e6d28c00",
   "metadata": {},
   "source": [
    "\n",
    "### 七、子串替换\n",
    "\n",
    "str.replace(str1, str2, times)\t子串 str1 替换为 str2，可指定替换次数 times（默认全替换）\n",
    "\n",
    "str.maketrans(str1, str2)\t生成字符映射表（str1 字符对应 str2 同位置字符，供 translate 使用）\n",
    "\n",
    "str.translate(table)\t基于映射表 table 批量单字符替换（需用 str.maketrans 生成映射表）\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "a42d8a27",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "shengdeyunlong\n",
      "----------------------------------------------------------------------------------------------------\n",
      "shengdeyunlong\n",
      "shengdeyunlong\n",
      "----------------------------------------------------------------------------------------------------\n",
      "shengde.yunlong\n",
      "shengde.yunlong\n",
      "----------------------------------------------------------------------------------------------------\n",
      "syengdeymnlong\n",
      "shengdehunlong\n"
     ]
    }
   ],
   "source": [
    "a = \"shengdeyunlong\" # 无空格，替换无效果\n",
    "print (a.replace (' ', '.')) # 输出：shengdeyunlong\n",
    "print ('--'*50)\n",
    "a = \"shengdeyunlong\" # 末尾 1 个空格\n",
    "print (a.replace (' ', '.')) # 替换所有空格，输出：shengdeyunlong.\n",
    "print (a.replace (' ', '.', 3)) # count=3 但仅 1 个空格，输出同上\n",
    "print ('--'*50)\n",
    "a = \"shengde yunlong\" # 共 3 个空格（开头 / 中间 / 末尾）\n",
    "print (a.replace (' ', '.', 2)) # 替换前 2 个空格，输出：.shengde.yunlong （末尾空格留）\n",
    "print (a.replace (' ', '.', 3)) # 替换全部 3 个空格，输出：.shengde.yunlong.\n",
    "print ('--'*50)\n",
    "a=\"syengdeymnlong\"\n",
    "print(a)\n",
    "table=a.maketrans('ym','hu')\n",
    "print(a.translate(table)) #对应的字符一个一个替换掉，y对应h，m对应u"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "67aa3ba3",
   "metadata": {},
   "source": [
    "### 八. 拆分列表\n",
    "str.split(str1,num)\n",
    "\n",
    "str.rsplit(str1,num)\n",
    "\n",
    "介绍拆分列表用法：从左端或者右边开始拆分列表，并指定拆分次数。\n",
    "\n",
    "str.partition(str1)\n",
    "\n",
    "介绍partition用法：返回一个元组，包含拆分字符串前后的字符串。\n",
    "\n",
    "str.rpartition(str1)\n",
    "\n",
    "str.join(seq)\n",
    "\n",
    "介绍join用法：将列表中的元素以指定的字符连接生成一个新的字符串。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "495ad3eb",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['sheng', 'de', 'yun', 'long']\n",
      "['sheng', 'de', 'yun long']\n",
      "('sheng ', 'de', ' yun long')\n",
      "张三+李四+王二麻子\n"
     ]
    }
   ],
   "source": [
    "a=\"sheng de yun long\"\n",
    "print(a.split(' '))\n",
    "print(a.split(' ',2))#只拆分两次\n",
    "print(a.partition('de'))\n",
    "a='+'\n",
    "print(a.join(['张三', '李四', '王二麻子'])) #最后一个元素不加连接符 ,join里面的是被链接的元素，a是连接符\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "03f5ccea",
   "metadata": {},
   "source": [
    "### 九：判断格式\n",
    "str.isalnum()：判断字符串是否只包含数字和字母，并且非空\n",
    "\n",
    "str.isalpha()：判定字符串是否只包含字符，并且非空\n",
    "\n",
    "str.isdigit()：判定字符串是否全为数字字符（汉字数字无法识别）\n",
    "\n",
    "str.isnumeric()：判定字符串是否全为数字字符（汉字数字也可识别）\n",
    "\n",
    "str.isidentifier()：判断字符串是否为合法的 Python 标识符\n",
    "\n",
    "str.istitle()：判定字符串的单词是否首字母大写\n",
    "\n",
    "str.isupper()：判定字符串是否全为大写字符\n",
    "\n",
    "str.islower()：判定字符串是否全为小写字符\n",
    "\n",
    "str.isspace()：判定字符串是否全为空白字符\n",
    "\n",
    "str.count(str1)：用于计算字符串中子串 str1 的出现次数，该方法也可用于列表或元组，统计元素出现的频次\n",
    "\n",
    "max()：返回字符串中字符的最大值（按 Unicode 编码排序）。\n",
    "\n",
    "min()：返回字符串中字符的最小值（按 Unicode 编码排序）"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8b01f693",
   "metadata": {},
   "source": [
    "### 十：字符串格式化输出(format)\n",
    "format()函数：\n",
    "在使用之前需要一对{}进行占位，按照参数的默认顺序赋值\n",
    "\n",
    "也可以按照序号指定赋值\n",
    "\n",
    "#### type\n",
    "s\t要求任意数据类型，功能是将数据转换为字符串（默认格式，可省略）\n",
    "\n",
    "b\t要求输入为整数（int），功能是将整数转换为二进制字符串\n",
    "\n",
    "c\t要求输入为整数（Unicode 码点），功能是将整数转换为对应 Unicode 字符\n",
    "\n",
    "d\t要求输入为整数（int），功能是将整数格式化为十进制字符串\n",
    "\n",
    "o\t要求输入为整数（int），功能是将整数转换为八进制字符串（无前缀0o）\n",
    "\n",
    "x/X\t要求输入为整数（int），功能是将整数转换为十六进制字符串（x小写a-f，X大写A-F，无前缀0x）\n",
    "\n",
    "e/E\t要求输入为整数 / 浮点数（int/float），功能是将数值以科学计数法表示（e小写e，E大写E）\n",
    "\n",
    "f\t要求输入为整数 / 浮点数（int/float），功能是将数值格式化为固定小数位数的浮点数（默认 6 位，可指定位数如.2f）\n",
    "\n",
    "g/G\t要求输入为整数 / 浮点数（int/float），功能是自动选择f（普通浮点数）或e（科学计数法），并去除多余末尾 0（G对应E）\n",
    "\n",
    "%\t要求输入为整数 / 浮点数（int/float），功能是将数值乘 100 后以百分比形式显示（默认 6 位小数，可指定位数如.1%）\n",
    "\n",
    "#### 字段宽度的设置\n",
    "\n",
    "\"{num:10}\".fomat(num=3);关键词\"num\"为字段名，\"10\"为字段宽度，\"3\"为字段内容。\n",
    "\n",
    "假如出现了num:10.2就是说字段是10位，小数点后2位。\n",
    "\n",
    "左对齐：\"{num:'在这里可以添加想要添加的字符不一定是空格'<10}\".fomat(num=3);\n",
    "\n",
    "右对齐：\"{num:>10}\".fomat(num=3);\n",
    "\n",
    "居中：\"{num:^10}\".fomat(num=3);\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "6fbfa99f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hello world !\n",
      "hello world world\n",
      "1010 b 1011\n",
      "The number is A\n",
      "alpha{ 1, b}cd\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "'         3'"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "print(\"{} {} {}\".format(\"hello\",\"world\",\"!\"));\n",
    "print(\"{0} {1} {1}\".format(\"hello\",\"world\",\"!\"));\n",
    "print(\"{0:b} {1:s} {2:b}\".format(10,'b',11))\n",
    "print(\"The number is {0:c}\".format(65))\n",
    "result = \"alpha{{{a: b}, {0}}}{1}{2}\".format( \"b\", \"c\", \"d\",a=1)#在 Python 的 str.format() 方法中，位置参数必须放在关键字参数之前，不能将关键字参数（如 a=1）放在位置参数（如 \"b\"、\"c\"）的前面。\n",
    "print(result)\n",
    "\"{0:10}\".format(3)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "02ca9901",
   "metadata": {},
   "source": [
    "#### f关键字：format的缩写符"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "8d1cadfe",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Sdyl, I'd like to invite you to dinner at my place.\n"
     ]
    }
   ],
   "source": [
    "name=input(\"What is your name? \")\n",
    "print(f\"{name.title()}, I'd like to invite you to dinner at my place.\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "a84a34b6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1×1= 1    1×2= 2    1×3= 3    1×4= 4    1×5= 5    1×6= 6    1×7= 7    1×8= 8    1×9= 9    \n",
      "2×2= 4    2×3= 6    2×4= 8    2×5=10    2×6=12    2×7=14    2×8=16    2×9=18    \n",
      "3×3= 9    3×4=12    3×5=15    3×6=18    3×7=21    3×8=24    3×9=27    \n",
      "4×4=16    4×5=20    4×6=24    4×7=28    4×8=32    4×9=36    \n",
      "5×5=25    5×6=30    5×7=35    5×8=40    5×9=45    \n",
      "6×6=36    6×7=42    6×8=48    6×9=54    \n",
      "7×7=49    7×8=56    7×9=63    \n",
      "8×8=64    8×9=72    \n",
      "9×9=81    \n"
     ]
    }
   ],
   "source": [
    "for i in range(1, 10):\n",
    "    line = \"\"\n",
    "    for j in range(i, 10):\n",
    "        # 直接在字符串中计算、判断，无需额外变量\n",
    "        line = f\"{i}×{j}={i*j:>2}  \" # 结果占2位，右对齐\n",
    "        print(line,end='  ')\n",
    "    print()#print自己会执行一次就换行\n",
    "    "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7c6ad6e2",
   "metadata": {},
   "source": [
    "# 序列"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "452ce1b7",
   "metadata": {},
   "source": [
    "## 列表\n",
    "\n",
    "### 列表的创建\n",
    "定义：一种有序可变的序列，类似数组[]，里面存放的是任意类型的数据。\n",
    "创建方式：\n",
    "1. 通过[]创建\n",
    "2. 通过list()创建:必须传入一个「可迭代对象」作为参数（不能传入多个独立元素）\n",
    "\n",
    "什么是 “可迭代对象”？\n",
    "\n",
    "可迭代对象是指能够被遍历的对象，常见的包括：字符串、元组、列表、字典（默认迭代键）、集合、range 对象等。简单说，“可迭代” 意味着对象中包含多个元素，且可以逐个取出。\n",
    "\n",
    "list() 的工作逻辑   \n",
    "\n",
    "list() 会将传入的可迭代对象中的每个元素依次取出，组成一个新的列表。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "138bf447",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "20\n",
      "张三\n",
      "177.6\n",
      "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]\n",
      "{0 1 2 3 4 5 6 7 8 9 }\n",
      "['s', 'h', 'e', 'n', 'd', 'g', 'e', 'y', 'u', 'n', 'l', 'o', 'n', 'g']\n"
     ]
    }
   ],
   "source": [
    "lb=[]\n",
    "lb=[20,'张三',177.6]\n",
    "for i in range(0,len(lb)):\n",
    "    print(lb[i])\n",
    "lc=list(range(0,10))    \n",
    "print(lc)\n",
    "print('{',end='');\n",
    "for i in lc:\n",
    " print(f\"{i } \" ,end='') \n",
    "print('}') \n",
    "# 给字符串加引号，使其成为有效的可迭代对象\n",
    "m = list(\"shendgeyunlong\")  # 注意字符串必须用引号包裹\n",
    "print(m)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "27daaed2",
   "metadata": {},
   "source": [
    "### 列表访问\n",
    "访问某一个元素：列表创建完，会开辟一个连续的内存空间，然后把列表的元素依次填入这个连续空间中。索引方式分为正向索引和反向索引。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "9df885e7",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1 2 3 4 5 6 7 8 9 9\n"
     ]
    }
   ],
   "source": [
    "la=list(range(1,10))\n",
    "for i in la :\n",
    "    print(i,end=' ')\n",
    "print(la[-1])\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4324d4d6",
   "metadata": {},
   "source": [
    "多个元素访问：\n",
    "列表切片：[start:end:step]起始和终止的位置，然后设置步长 \n",
    "\n",
    "如果缺少start，默认为0\n",
    "\n",
    "如果缺少end，默认为列表末尾\n",
    "\n",
    "如果缺少step，默认为1\n",
    "\n",
    "如果只有[:]则访问所有元素"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "04d9694c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[4, 5, 6, 7]\n"
     ]
    }
   ],
   "source": [
    "la=list(range(1,10))\n",
    "print(la[3:7:1])#左开右闭"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "947552db",
   "metadata": {},
   "source": [
    "### 列表元素增删\n",
    "append:list.append(x)//list是一个列表，x是一个元素，将x添加到列表的末尾\n",
    "\n",
    "insert:list.insert(i,x)//list是一个列表，i是索引，x是一个元素，将x插入到列表的索引i的位置\n",
    "\n",
    "delete:del list[i]//list是一个列表，i是索引，将列表的索引i处的元素删除\n",
    "\n",
    "extend:list.extend(x)//list是一个列表，x是一个列表，将列表x的元素添加到列表list的末尾\n",
    "\n",
    "pop:list.pop(i)//list是一个列表，i是索引，将列表的索引i处的元素删除并返回该元素,如果没有i索引，则返回最后一个元素\n",
    "\n",
    "remove:list.remove(x)//list是一个列表，x是一个元素，将列表中第一个等于x的元素删除"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "f65a091d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "当前i的值：1\n",
      "[1, 1, 1, 1, 1, 1, 1, 1, 1]\n",
      "[1, 1, 1, 1, 1, 1, 1, 1]\n",
      "修改后i的值：0\n",
      "\n",
      "当前i的值：1\n",
      "[1, 1, 1, 1, 1, 1, 1, 1]\n",
      "[1, 1, 1, 1, 1, 1, 1]\n",
      "修改后i的值：0\n",
      "\n",
      "当前i的值：1\n",
      "[1, 1, 1, 1, 1, 1, 1]\n",
      "[1, 1, 1, 1, 1, 1]\n",
      "修改后i的值：0\n",
      "\n",
      "当前i的值：1\n",
      "[1, 1, 1, 1, 1, 1]\n",
      "[1, 1, 1, 1, 1]\n",
      "修改后i的值：0\n",
      "\n",
      "当前i的值：1\n",
      "[1, 1, 1, 1, 1]\n",
      "[1, 1, 1, 1]\n",
      "修改后i的值：0\n",
      "\n"
     ]
    }
   ],
   "source": [
    "la = []\n",
    "for i in range(1,10):\n",
    "    la.append(1)\n",
    "\n",
    "for i in la:\n",
    "    print(f\"当前i的值：{i}\")  # 打印迭代变量i\n",
    "    print(la)\n",
    "    la.remove(1)\n",
    "    print(la)\n",
    "    i = 0  # 尝试修改i\n",
    "    print(f\"修改后i的值：{i}\\n\")  # 虽然i变了，但不影响下一次循环,无论怎么修改 i 的值，迭代器的 “进度” 不会被重置\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b4390087",
   "metadata": {},
   "source": [
    "### 列表与列表之间的操作\n",
    "列表之间的加法：\n",
    "\n",
    "a=[1,2,3]\n",
    "\n",
    "b=[4,5,6]\n",
    "\n",
    "a+=b; --> a=[1,2,3,4,5,6]\n",
    "\n",
    "但是必须是同类型的序列\n",
    "\n",
    "列表的数乘：\n",
    "\n",
    "a=[1,2,3]\n",
    "\n",
    "a*=2; --> a=[1,2,3,1,2,3]\n",
    "\n",
    "list.sort():对该列表进行排序，默认是升序排列\n",
    "\n",
    "reverse=True:降序排列\n",
    "\n",
    "list.reverse():对列表进行倒序排列\n",
    "\n",
    "list.clear():清空列表"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "6c8d02ff",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 3, 4, 5, 6]\n",
      "[1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6]\n",
      "['de', 'long', 'sheng', 'yun']\n"
     ]
    }
   ],
   "source": [
    "a = [1, 2, 3]\n",
    "b = [4, 5, 6]\n",
    "a += b  # 将b的元素合并到a中\n",
    "print(a)  # 输出：[1, 2, 3, 4, 5, 6]\n",
    "a = a * 5  # 列表重复5次\n",
    "print(a)  # 输出：[1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6, ...]（共5组原列表元素）\n",
    "# 修正1：用[]直接创建多元素列表（正确方式）\n",
    "m = ['sheng', 'de', 'yun', 'long']\n",
    "m.sort()  # 按逆序排序\n",
    "print(m)  # 输出：['yun', 'sheng', 'long', 'de']（按字母升序排列）"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f22100a4",
   "metadata": {},
   "source": [
    "list.index(value,start，end)指定value在列表中的索引，start和end指定列表的开始和结束索引。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "cc02e2c2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "索引的位置是: 1\n"
     ]
    }
   ],
   "source": [
    "la=[\"sheng\",\"de\",\"yun\",\"long\",'de']\n",
    "print(\"索引的位置是:\",la.index('de',0))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "45cac9f3",
   "metadata": {},
   "source": [
    "### 判断列表元素大小与排序\n",
    "\n",
    "min(list,key=xx) 获取最小元素，按照key的返回值获取\n",
    "\n",
    "max(list,key=xx) 获取最大元素，按照key的返回值获取\n",
    "\n",
    "sum(list) 得到元组的和\n",
    "\n",
    "sorted(list,key=xx，reverse=True) 对列表进行排序，按照key的返回值进行排序,逆序\n",
    "\n",
    "key:在 sorted()、list.sort()、max()、min() 等函数中，key 是一个可选参数，用于指定 “排序 / 筛选的规则”（即按什么标准处理元素）。\n",
    "\n",
    "key 接受一个函数作为值，该函数会对每个元素进行处理，返回一个 “用于比较的值”，最终函数会根据这个返回值进行排序 / 筛选。\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "8393370c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['sheng', 'long', 'yun', 'de']\n",
      "sheng\n"
     ]
    }
   ],
   "source": [
    "numbers=['sheng','de','yun','long']\n",
    "print(sorted(numbers,key=len,reverse=True))\n",
    "print(max(['sheng','de','yun','long'],key=len))#按照长度"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0b830d6d",
   "metadata": {},
   "source": [
    "zip(list1,list2,list3,...,listn),生成一个zip对象，其每个元素为列表中对应位置的元素形成的元素，当序列长度\n",
    "不同时，zip最短的序列长度为zip的长度。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "08f1727e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "张三 身高 170 年龄 17\n",
      "王五 身高 180 年龄 18\n",
      "小明 身高 160 年龄 16\n",
      "----------------------------------------------------------------------------------------------------\n",
      "张三 170 17\n",
      "王五 180 18\n"
     ]
    }
   ],
   "source": [
    "names=['张三','王五','小明']\n",
    "talls=['170','180','160']\n",
    "ages=['17','18','16']\n",
    "for ages ,talls,names in zip(ages,talls,names):\n",
    "    print(names,'身高',talls,'年龄',ages)\n",
    "print('--'*50)  \n",
    "names=['张三','王五','小明']\n",
    "talls=['170','180','160']\n",
    "ages=['17','18']\n",
    "for ages ,talls,names in zip(ages,talls,names):\n",
    "    print(names,talls,ages)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6dd3d871",
   "metadata": {},
   "source": [
    "### 列表推导式，利用for循环，从已有序列中快速生成新的序列。\n",
    "\n",
    "语法结构：\n",
    "\n",
    "[表达式for表达式中的变量 in 可迭代对象 if条件]\n",
    "\n",
    "最后功能相当于append()方法。    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "d31516a6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0, 1, 4, 9, 16, 25, 36, 49]\n"
     ]
    }
   ],
   "source": [
    "squares = []\n",
    "for i in range(10):\n",
    "    squares.append(i **2)  # 表达式：i的平方\n",
    "squares = [i** 2 for i in range(8)]\n",
    "print(squares)  # 输出：[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b1173756",
   "metadata": {},
   "source": [
    "## 元组\n",
    "\n",
    "和列表不同的是元组是不可变序列，一旦创建，不可修改其中的元素。\n",
    "\n",
    "### 元组的创建方式\n",
    "\n",
    "1. 使用圆括号创建元组。\n",
    "\n",
    "2. 使用tuple()函数将列表，range对象，字符串，或其他可迭代对象转换为元组。\n",
    "\n",
    "\n",
    "注：del删除不可以删除单独元素，只能删除整个元组。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "79b50924",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "('shengdeyunlong',)\n",
      "----------------------------------------------------------------------------------------------------\n",
      "shengdeyunlong\n",
      "----------------------------------------------------------------------------------------------------\n",
      "('sheng', 'de', 'yun', 'long')\n",
      "----------------------------------------------------------------------------------------------------\n",
      "sheng\n",
      "(['sheng', 'de', 'yun', 'long'], 18)\n",
      "(['sheng', 'de', 'yun', 'long', 'deyunlong'], 18)\n"
     ]
    }
   ],
   "source": [
    "ta=(\"shengdeyunlong\",)#单个元素元组，后面需要加逗号\n",
    "print(ta)\n",
    "print('--'*50)\n",
    "ta=(\"shengdeyunlong\")#这就不是元组了，只是对ta这个变量进行赋值\n",
    "print(ta)\n",
    "print('--'*50)\n",
    "ta=(\"sheng\",\"de\",\"yun\",\"long\")\n",
    "print(ta)\n",
    "print('--'*50)\n",
    "print(ta[0])\n",
    "#ta=(\"sheng\",\"de\",\"yun\",\"long\")\n",
    "# ta[0].append(\"deyunlong\")报错了,元组不可变\n",
    "ta=([\"sheng\",\"de\",\"yun\",\"long\",],18)\n",
    "print(ta)\n",
    "ta[0].append(\"deyunlong\")#元组嵌套列表，列表可以改变\n",
    "print(ta)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "ab71fa2d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768, 65536, 131072, 262144, 524288, 1048576, 2097152, 4194304, 8388608, 16777216, 33554432, 67108864, 134217728, 268435456, 536870912, 1073741824, 2147483648, 4294967296, 8589934592, 17179869184, 34359738368, 68719476736, 137438953472, 274877906944, 549755813888, 1099511627776, 2199023255552, 4398046511104, 8796093022208, 17592186044416, 35184372088832, 70368744177664, 140737488355328, 281474976710656, 562949953421312, 1125899906842624, 2251799813685248, 4503599627370496, 9007199254740992, 18014398509481984, 36028797018963968, 72057594037927936, 144115188075855872, 288230376151711744, 576460752303423488, 1152921504606846976, 2305843009213693952, 4611686018427387904, 9223372036854775808] \n",
      " 总的小麦粒子数 18446744073709551615\n",
      "18446744073709551615\n"
     ]
    }
   ],
   "source": [
    "la = []\n",
    "for i in range(64):\n",
    "    la.append(2**i)\n",
    "print(la,'\\n' ,\"总的小麦粒子数\", sum(la))\n",
    "\n",
    "sum=0;\n",
    "for i in range(64):\n",
    "    sum+=(2**i)\n",
    "print(sum)    \n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3439adba",
   "metadata": {},
   "source": [
    "# 集合"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0c513399",
   "metadata": {},
   "source": [
    "特点:数据内容不能存在重复，并且集合中的元素是无序且元素只能是固定的数据类型的时候，使用集合来实现"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ec8b1ca1",
   "metadata": {},
   "source": [
    "### 集合的创建\n",
    "1.直接创建集合\n",
    "\n",
    "直接通过大括号创建集合，但是不能创建空集合\n",
    "\n",
    "2.使用set函数创建集合\n",
    "\n",
    "内部需要是可迭代对象"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e792f2d1",
   "metadata": {},
   "source": [
    "### 集合的运算\n",
    "交集: \n",
    "a.intersection(b)//集合a和b的交集\n",
    "\n",
    "并集: \n",
    "a.union(b)//集合a和b的并集\n",
    "\n",
    "差集:\n",
    "a.difference(b)//集合a和b的差集\n",
    "\n",
    "对称差集:\n",
    "a.symmetric_difference(b)//集合a和b的差集"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "2f734aba",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "False\n"
     ]
    }
   ],
   "source": [
    "a={1,2,3}\n",
    "b={1,2,3,4,5}\n",
    "print(a>b)\n",
    "#集合 a 必须包含集合 b 的所有元素（包含关系）；\n",
    "#集合 a 的元素个数严格大于集合 b 的元素个数。"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "base",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.13.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
